import { useRoleStore } from "@/store"
import type { Ref } from "vue"
import type { CommonTypeItem, CommonDataItem } from "@/store/modules/role/types"
import { constant } from "lodash"
interface DialogInject {
  addRoleForm: Ref<{name: string, description: string, permIds: number[]}>
  checkall: Ref<boolean>
  indeterminate: Ref<boolean>
  setCheckVal: (val: boolean) => void
  setIndetVal: (val: boolean) => void
}
const roleStore = useRoleStore()
export const switchData = (checked: boolean, current: CommonTypeItem, dialogState: DialogInject) => {
  
  // 三种情况
  // 当前点击的第一层
  if (current.level === 1) {
    current.indeterminate = false
    // 获取当前所在的所有子级
    const result: CommonTypeItem[] = []
    getCurrentAllData(roleStore.allDataConfig, current, result, true)
    // 将所有子级的check变成选中
    switchChildStatus(result, checked)
    // 获取id集合
    const ids = result.map(item => item.id)
    // 将id插入表单
    switchFormIds([...ids, current.id], checked, dialogState.addRoleForm.value)
  }

  // 当前点击的是中间层
  if (current.level > 1 && current.level < 5) {
    current.indeterminate = false
    const result: CommonTypeItem[] = []
    getCurrentAllData(roleStore.allDataConfig, current, result, true)
    // 将所有子级的check变成选中
    switchChildStatus(result, checked)
    // 获取id集合
    const ids = result.map(item => item.id)
    if (current.level === 2) {
      const moduleCurrent = roleStore.moduleArr[current.parentCode!]
      setFatherStatus(moduleCurrent, roleStore.routerArr[current.parentCode!])
      // 将id插入表单
      if (checked) {
        switchFormIds([...ids, current.id, moduleCurrent.id], checked, dialogState.addRoleForm.value)
      } else {
        switchFormIds([...ids, current.id], checked, dialogState.addRoleForm.value)
        const routerChildCheck = roleStore.routerArr[current.parentCode!].map(item => item.checked).filter(Boolean)
        const routerChildIndet = roleStore.routerArr[current.parentCode!].map(item => item.indeterminate).filter(Boolean)
        routerChildCheck.length === 0 && routerChildIndet.length === 0 && switchFormIds([moduleCurrent.id], checked, dialogState.addRoleForm.value)
      }
    }
    if (current.level === 3) {
      const routerCurrent = roleStore.allDataConfig.filter(item => item.code === current.parentCode)[0]
      setFatherStatus(routerCurrent, roleStore.pageArr[current.parentCode!])
      const moduleCurrent = roleStore.moduleArr[routerCurrent.parentCode!]
      setFatherStatus(moduleCurrent, roleStore.routerArr[routerCurrent.parentCode!])
      // 将id插入表单
      if (checked) {
        switchFormIds([...ids, current.id, routerCurrent.id, moduleCurrent.id], checked, dialogState.addRoleForm.value)
      } else {
        switchFormIds([...ids, current.id], checked, dialogState.addRoleForm.value)
        const pageChildCheck = roleStore.pageArr[current.parentCode!].map(item => item.checked).filter(Boolean)
        const pageChildIndet = roleStore.pageArr[current.parentCode!].map(item => item.indeterminate).filter(Boolean)
        pageChildCheck.length === 0 && pageChildIndet.length === 0 && switchFormIds([routerCurrent.id], checked, dialogState.addRoleForm.value)
        const routerChildCheck = roleStore.routerArr[routerCurrent.parentCode!].map(item => item.checked).filter(Boolean)
        const routerChildIndet = roleStore.routerArr[routerCurrent.parentCode!].map(item => item.indeterminate).filter(Boolean)
        routerChildCheck.length === 0 && routerChildIndet.length === 0 && switchFormIds([moduleCurrent.id], checked, dialogState.addRoleForm.value)
      }
    }
  }

  // 当前点击的最后一层
  if (current.level === 5) {
    // 获取当前所在的链
    const result: CommonTypeItem[] = []
    getCurrentAllData(roleStore.allDataConfig, current, result)
    // 将所有父级的indeterminate变成选中
    switchChildStatus(result, checked, true)
    // 获取当前所在的所有子级
    const specialChilds: CommonTypeItem[] = []
    getSpecialChilds(roleStore.allDataConfig, current, specialChilds)
    const componentItem = roleStore.allDataConfig.filter(item => item.code === current.parentCode)[0]
    const pageCurrent = roleStore.allDataConfig.filter(item => item.code === componentItem.parentCode)[0]
    const allChilds: CommonTypeItem[] = []
    specialChilds.map(item => allChilds.push(...roleStore.detailArr[item.code]))
    if (specialChilds.length === 0) {
      // 不是特殊的一层
      setFatherStatus(pageCurrent, roleStore.detailArr[current.parentCode!])
    } else {
      // 特殊的一层
      setFatherStatus(pageCurrent, allChilds)
    }
    const routerCurrent = roleStore.allDataConfig.filter(item => item.code === pageCurrent.parentCode)[0]
    setFatherStatus(routerCurrent, roleStore.pageArr[pageCurrent.parentCode!])
    const moduleCurrent = roleStore.allDataConfig.filter(item => item.code === routerCurrent.parentCode)[0]
    setFatherStatus(moduleCurrent, roleStore.routerArr[routerCurrent.parentCode!])
    // 获取id集合
    const ids = result.map(item => item.id)
    // 将id插入表单
    checked && switchFormIds([...ids, current.id], checked, dialogState.addRoleForm.value)
    if (!checked) {
      switchFormIds([current.id], checked, dialogState.addRoleForm.value)
      const lastChildCheck = roleStore.detailArr[current.parentCode!].map(item => item.checked).filter(Boolean)
      lastChildCheck.length === 0 && switchFormIds([componentItem.id], checked, dialogState.addRoleForm.value)
      specialChilds.length === 0 && lastChildCheck.length === 0 && switchFormIds([ pageCurrent.id], checked, dialogState.addRoleForm.value)
      if (specialChilds.length !== 0) {
        const lastAllChildCheck = allChilds.map(item => item.checked).filter(Boolean)
        lastAllChildCheck.length === 0 && switchFormIds([pageCurrent.id], checked, dialogState.addRoleForm.value)
      }
      const pageChildCheck = roleStore.pageArr[pageCurrent.parentCode!].map(item => item.checked).filter(Boolean)
      const pageChildIndet = roleStore.pageArr[pageCurrent.parentCode!].map(item => item.indeterminate).filter(Boolean)
      pageChildCheck.length === 0 && pageChildIndet.length === 0 && switchFormIds([routerCurrent.id], checked, dialogState.addRoleForm.value)
      const routerChildCheck = roleStore.routerArr[routerCurrent.parentCode!].map(item => item.checked).filter(Boolean)
      const routerChildIndet = roleStore.routerArr[routerCurrent.parentCode!].map(item => item.indeterminate).filter(Boolean)
      routerChildCheck.length === 0 && routerChildIndet.length === 0 && switchFormIds([moduleCurrent.id], checked, dialogState.addRoleForm.value)
    }
  }
  // console.log(dialogState.addRoleForm.value.permIds.length, "长度");
  // 处理全选操作
  const moduleArrCheck = Object.values(roleStore.moduleArr).map(item => item.checked).filter(Boolean)
  const moduleArrIndet = Object.values(roleStore.moduleArr).map(item => item.indeterminate).filter(Boolean)
  const checkAllRes = getStatus(roleStore.moduleArr, moduleArrIndet, moduleArrCheck, true)
  dialogState.setCheckVal(checkAllRes.checked)
  dialogState.setIndetVal(checkAllRes.indeterminate)
}

/**
 * 查找当前树的所有父级或者当前树的所有子级
 * @param data 当前数据查找源，全量数据  全量数据即store中allDataConfig的数据
 * @param current 当前操作的一项
 * @param result 处理后的结果
 * @param isRight 树的指针从顶找还是从末尾找
 */
export const getCurrentAllData = (data: CommonTypeItem[], current: CommonTypeItem, result: CommonTypeItem[], isRight: boolean = false) => {
  data.map(item => {
    // 找子级
    isRight && item.parentCode === current.code && result.push(item) && getCurrentAllData(data, item, result, isRight)
    // 找父级
    !isRight && item.code === current.parentCode && result.push(item) && getCurrentAllData(data, item, result, isRight)
  })
}

/**
 * 处理标注配置的特殊层级方法
 * @param data 当前数据查找源，全量数据  全量数据即store中allDataConfig的数据
 * @param current 当前操作的一项
 * @param result 处理后的结果 当前所有子级数组
 */
export const getSpecialChilds = (data: CommonTypeItem[], current: CommonTypeItem, result: CommonTypeItem[]) => {
  data.map(item => {
    if (item.code === current.parentCode) {
      if (item.level === 3 && item.children.length > 1) {
        // 特殊的一项
        const arr = data.filter(t => t.parentCode === item.code)
        result.push(...arr)
      } else {
        getSpecialChilds(data, item, result)
      }
    }
  })
}

/**
 * 改变当前所有子项的选中状态函数
 * @param result 需要处理的数据集合
 * @param checked 当前复选框状态值
 */
export const switchChildStatus = (result: CommonTypeItem[], checked: boolean, isLast: boolean = false) => {
  if (isLast) {
    result.map(item => item.indeterminate = true)
  } else {
    result.map(item => {
      item.level !== 4 && (item.indeterminate = false)
      item.checked = checked
    })
  }
}

/**
 * 通过当前点击的一项获取下层所有子级数据
 * @param childs 所有子级的结果数据
 * @param currentItem 当前自己的所在子级数组
 * @param allData 当前数据查找源，全量数据  全量数据即store中allDataConfig的数据
 */
export const currentItemChilds = (childs: CommonDataItem[], currentItem: CommonDataItem[], allData: {
  [x: string]: CommonDataItem[];
}) => {
  currentItem && currentItem.map(item => {
    childs.push(item)
    currentItemChilds(childs, allData[item.code], allData)
  })
}

/**
 * 改变表单id字段集合函数
 * @param ids 需要插入的id集合
 * @param checked 当前复选框状态值
 * @param result 需要操作的表单集合
 */
export const switchFormIds = (ids: number[], checked: boolean, form: {
  name: string;
  description: string;
  permIds: number[];
}) => {
  if (checked) {
    // result = [...result, ...ids]
    ids.map(item => {
      !form.permIds.includes(item) && form.permIds.push(item)
    })
  } else {
    form.permIds = form.permIds.filter(item => !ids.includes(item))
    // ids.map(item => {
    //   ids.includes(item) && result.splice(item)
    // })
  }
}

/**
 * 通过数据判定当前应该选中还是半选辅助函数
 * @param stateData 当前仓库中state中的数据集合
 * @param indetArr 当前半选状态布尔值集合
 * @param checkArr 当前选中状态布尔值集合
 * @returns 当前选中，半选的结果
 */
export const getStatus = (stateData: any, indetArr: (boolean | undefined)[], checkArr: boolean[], isAll: boolean = false): {checked: boolean, indeterminate: boolean} => {
  let result = {
    checked: false,
    indeterminate: false
  }
  if (isAll) {
    // 全选时获取状态值
    if (indetArr.length === 0) {
      result.indeterminate = checkArr.length !== 0 && checkArr.length < Object.values(stateData).length
    } else {
      result.indeterminate = indetArr.length >= 1 && indetArr.length < Object.values(stateData).length
    }
    result.checked = checkArr.length === Object.values(stateData).length
  } else {
    // 除全选外其他情况获取状态值
    if (indetArr.length === 0) {
      result.indeterminate = checkArr.length !== 0 && checkArr.length < stateData.length
    } else {
      result.indeterminate = indetArr.length >= 1 && indetArr.length <= stateData.length
    }
    result.checked = checkArr.length === stateData.length
  }
  return result
}

/**
 * 设置当前父级节点状态值辅助函数
 * @param current 当前需要改变节点对象
 * @param currentChilds 当前的子级集合
 */
export const setFatherStatus = (current: CommonTypeItem, currentChilds: CommonTypeItem[], isLast: boolean = false) => {
  if (isLast) {
    // 是最后一层
    const tempCheckArr = currentChilds.map(item => item.checked).filter(Boolean)
    current.checked = tempCheckArr.length === currentChilds.length
    current.indeterminate = tempCheckArr.length !== 0 && tempCheckArr.length < currentChilds.length
  } else {
    // 不是最后一层
    const tempCheckArr = currentChilds.map(item => item.checked).filter(Boolean)
    const tempIndetArr = currentChilds.map(item => item.indeterminate).filter(Boolean)
    const result = getStatus(currentChilds, tempIndetArr, tempCheckArr)
    current.checked = result.checked
    current.indeterminate = result.indeterminate
  }
}

/**
 * 权限树重置选择状态函数
 */
export const clearAllCheck = () => {
  roleStore.allDataConfig.map(item => {
    item.level === 1 && (item.isVisibile = true)
    item.indeterminate = false
    item.checked = false
  })
}

/**
 * 角色复选框树的数据回填函数
 * @param idArr 结果id数组集合
 * @param checkall 全选的选中状态值
 * @param indeterminate 全选的半选状态值
 */
export const setCheckedStatus = (idArr: number[], checkall: Ref<boolean>, indeterminate: Ref<boolean>) => {
  roleStore.allDataConfig.map(current => {
    current.checked = idArr.includes(current.id) && current.level === 5
    if (idArr.includes(current.id) && current.level === 5) {
      const specialChilds: CommonTypeItem[] = []
      getSpecialChilds(roleStore.allDataConfig, current, specialChilds)
      const componentItem = roleStore.allDataConfig.filter(item => item.code === current.parentCode)[0]
      const pageCurrent = roleStore.allDataConfig.filter(item => item.code === componentItem.parentCode)[0]
      const allChilds: CommonTypeItem[] = []
      specialChilds.map(item => allChilds.push(...roleStore.detailArr[item.code]))
      if (specialChilds.length === 0) {
        // 不是特殊的一层
        setFatherStatus(pageCurrent, roleStore.detailArr[current.parentCode!])
      } else {
        // 特殊的一层
        setFatherStatus(pageCurrent, allChilds)
      }
      const routerCurrent = roleStore.allDataConfig.filter(item => item.code === pageCurrent.parentCode)[0]
      setFatherStatus(routerCurrent, roleStore.pageArr[pageCurrent.parentCode!])
      const moduleCurrent = roleStore.allDataConfig.filter(item => item.code === routerCurrent.parentCode)[0]
      setFatherStatus(moduleCurrent, roleStore.routerArr[routerCurrent.parentCode!])
    }
  })
  // 处理全选操作
  const moduleArrCheck = Object.values(roleStore.moduleArr).map(item => item.checked).filter(Boolean)
  const moduleArrIndet = Object.values(roleStore.moduleArr).map(item => item.indeterminate).filter(Boolean)
  const checkAllRes = getStatus(roleStore.moduleArr, moduleArrIndet, moduleArrCheck, true)
  checkall.value = checkAllRes.checked
  indeterminate.value = checkAllRes.indeterminate
}